'use strict';
const mongoose = require('mongoose');
const dataApi=require('./common');

/* wm_posts table structure
 --------------------------------------------
 - post_title : 文章标题
 - post_excerpt : 文章描述
 - post_content : 文章内容
 - post_term : 文章分类
 - post_author : 文章作者
 - post_slug : 文章别名,短网址
 - post_type : 文章类型
 - post_status : 文章状态 -1=回收站,0=正常,1=草稿
 - post_date : 发布时间
 - post_modified : 更新时间
 --------------------------------------------
 */

var PostSchema = new mongoose.Schema({

  post_title: {
    type : String,
    required:true
  },

  post_excerpt: {
    type : String
  },

  post_content: {
    type: String,
    required: [true, '内容不能为空']
  },

  post_term: {
    type: mongoose.Schema.ObjectId,
    ref: 'term'
  },

  post_author: {
    type: mongoose.Schema.ObjectId,
    ref: 'user'
  },

  post_slug: {
    type: String,
    unique: true,
    required: true,
    validate: [
      {
        validator: function (val) {
          return /^(?![_|\-])(?!.*?[_|\-]$)[\w|\-|\u4e00-\u9fa5]+$/.test(val);
        },

        msg: '请输入正确自定义链接（支持数字英文中文，"_" "-" 不能以字符开始结尾）'
      }
    ]
  },

  post_type: {
    type: Number,
    default: 0
  },

  post_status: {
    type: Number,
    default: 0
  },

  post_date:  {
    type: String,
    default: Date.now
  },

  post_modified: {
    type: String,
    default: Date.now
  }

});

PostSchema.virtual('key').get(function () {
  return this._id;
});

PostSchema.set('toJSON', { virtuals: true })


dataApi(PostSchema,'post');

module.exports = mongoose.model('post', PostSchema);